/**
 * 
 */
package com.jonosoft.photocast.viewer.web.client.ui;

import java.util.Iterator;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.jonosoft.photocast.viewer.web.client.ImageCollection;
import com.jonosoft.photocast.viewer.web.client.photocast.Photocast;
import com.jonosoft.photocast.viewer.web.client.photocast.PhotocastController;
import com.jonosoft.photocast.viewer.web.client.photocast.PhotocastRecieveHandler;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsByMonthMenuItem;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsByTitleMenuItem;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsByYearMenuItem;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuBar;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem;
import com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem.CommandDelegate;
import com.jonosoft.photocast.viewer.web.client.util.Size;

/**
 * TODO Add description PhotocastViewer (com.jonosoft.photocast.viewer.web.client.PhotocastViewer)
 *
 * @author jon
 *
 */
public class PhotocastViewer extends Composite implements WindowResizeListener, CommandDelegate {
	
	private final FlexTable layoutTable = new FlexTable();
	private final AppToolbar appToolbar = new AppToolbar();
	private final ImageCollectionsMenuBar imageCollectionsMenuBar = new ImageCollectionsMenuBar(this);
	private final ImageBrowser imageBrowser = new ImageBrowser();
	private final PhotocastReciever photocastReciever = new PhotocastReciever();
	private final Size appToolbarSize = new Size(0, 140);
	private final Size imageCollectionsMenuBarSize = new Size(128, 0);
	private final Size imageBrowserSize = new Size();
	
    public PhotocastViewer() {
    	initWidget(layoutTable);
    	
    	layoutTable.setStyleName("photocast-viewer");
    	
    	layoutTable.setCellPadding(0);
    	layoutTable.setCellSpacing(0);

		layoutTable.insertRow(0);
		layoutTable.insertRow(0);
		layoutTable.addCell(0);
		layoutTable.addCell(1);
		layoutTable.addCell(1);
		layoutTable.getFlexCellFormatter().setColSpan(0, 0, 2);

		layoutTable.setWidget(0, 0, appToolbar);
		layoutTable.setWidget(1, 0, imageCollectionsMenuBar);
		layoutTable.setWidget(1, 1, imageBrowser);
		
		Window.addWindowResizeListener(this);
    	
    	Photocast photocast = PhotocastController.loadPhotocast("http://web.mac.com/skater0i/iPhoto/landon-pics-for-web/index.rss", photocastReciever);
    	
    	
		
    	//DeferredCommand.add(new DeferredResize(this));
    }

	private class PhotocastReciever implements PhotocastRecieveHandler {
		/**
		 * @see com.jonosoft.photocast.viewer.web.client.photocast.PhotocastRecieveHandler#onCompletion(com.jonosoft.photocast.viewer.web.client.photocast.Photocast)
		 */
		public void onCompletion(Photocast photocast) {
			ImageCollection imageCollection = ImageCollection.createFromPhotocast(photocast);
			
			imageCollectionsMenuBar.addImageCollection(imageCollection);
			
			imageBrowser.setImages(imageCollection);
		}
	}
	
	public void resize() {
		resize(new Size(Window.getClientWidth(), Window.getClientHeight()));
	}
	
	public void resize(Size size) {
    	//layoutTable.setWidth(size.getCSSWidth());
    	layoutTable.setHeight(size.getCSSHeight());
    	
    	appToolbarSize.setSize(0, AppToolbar.HEIGHT);
    	imageCollectionsMenuBarSize.setSize(ImageCollectionsMenuBar.WIDTH, 0);
		imageBrowserSize.setSize(0, size.getHeight().intValue()
		        - appToolbarSize.getHeight().intValue());
    	
    	appToolbar.resize(appToolbarSize);
    	imageCollectionsMenuBar.resize(imageCollectionsMenuBarSize);
    	imageBrowser.resize(imageBrowserSize);
    }

	/**
     * @see com.google.gwt.user.client.WindowResizeListener#onWindowResized(int, int)
     */
    public void onWindowResized(int width, int height) {
    	resize(new Size(width, height));
    }
    
    private class DeferredResize implements Command {

    	private PhotocastViewer photocastViewer = null;

    	public DeferredResize(PhotocastViewer photocastViewer) {
    		this.photocastViewer = photocastViewer;
    	}

    	public void execute() {
    		photocastViewer.resize(new Size(Window.getClientWidth(), Window.getClientHeight()));
    	}

    }

	/**
     * @see com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem.CommandDelegate#onExecute(com.jonosoft.photocast.viewer.web.client.ui.imagecollections.ImageCollectionsMenuItem)
     */
    public void onExecute(ImageCollectionsMenuItem menuItem) {
    	if (menuItem instanceof ImageCollectionsByTitleMenuItem) {
	        imageBrowser.setImages(menuItem.getImageCollection());
    	}
    	else if (menuItem instanceof ImageCollectionsByYearMenuItem) {
    		ImageCollection imageCollection = (ImageCollection) menuItem
			        .getImageCollection().clone();
			Iterator i = imageCollection.iterator();
			Integer year = new Integer(
			        ((ImageCollectionsByYearMenuItem) menuItem).getYear());
    		
    		//Window.alert(new Integer(imageCollection.size()).toString());
    		
    		while (i.hasNext()) {
    			LibraryImage image = (LibraryImage) i.next();
    			
    			if ((image.getPhotoDate().getYear() + 1900) != year.intValue())
    				i.remove();
    		}
    		
    		//Window.alert(new Integer(imageCollection.size()).toString());
    		
	        imageBrowser.setImages(imageCollection);
		}
		else if (menuItem instanceof ImageCollectionsByMonthMenuItem) {
			ImageCollection imageCollection = (ImageCollection) menuItem
			        .getImageCollection().clone();
			Iterator i = imageCollection.iterator();
			Integer year = new Integer(
			        ((ImageCollectionsByMonthMenuItem) menuItem).getYear());
			Integer month = new Integer(
			        ((ImageCollectionsByMonthMenuItem) menuItem).getMonth());
    		
    		while (i.hasNext()) {
    			LibraryImage image = (LibraryImage) i.next();
    			
    			if ((image.getPhotoDate().getYear() + 1900) != year.intValue() || image.getPhotoDate().getMonth() != month.intValue()) {
    				i.remove();
    			}
    		}
    		
	        imageBrowser.setImages(imageCollection);
    	}
    }
	
}
